//
//  MeViewController.m
//  intelligent
//
//  Created by 甘世清 on 2024/2/5.
//

#import "MeViewController.h"
#import "MeTableViewCell.h"
#import "TopMeView.h"
#import "LoginViewController.h"
#import "AboutViewController.h"
#import "FankuiViewController.h"
#import "HeaderTopView.h"
#import "QuitView.h"
#import "ZhuxiaoPopView.h"
#import "PeopleXYView.h"
#import "ZSViewController.h"
#import "CollectViewController.h"
@interface MeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic , strong) TopMeView * topView;
@property (nonatomic , strong) SQCustomButton * right;
@end

@implementation MeViewController
- (void)SQ_viewWillAppear:(BOOL)animated{
    [self SQ_Header];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"我的");
}

- (void)SQ_UIViewForSelfView{
    self.tableView.tableHeaderView = self.topView;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, FootHeight, 0));
    }];
    [self.zx_navBar addSubview:self.right];
    [self.right mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.zx_navTitleLabel);
            make.right.equalTo(self.zx_navBar.mas_right);
            make.width.mas_equalTo(120);
            make.height.mas_equalTo(40);
    }];
    WS(weakSelf);
    [self.right setTouchBlock:^(SQCustomButton * _Nonnull button) {
        ZSViewController * vc = [[ZSViewController alloc]init];
        [weakSelf SQ_pushViewControllerFull:vc];
    }];
    [self SQ_JsonArray];
}
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [HttpTool post:@"user/info" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        UserModel * mo = [UserModel share];
        UserModel* model = [UserModel mj_objectWithKeyValues:httpModel.data];
        model.token = mo.token;
        [UserModel updateLocalUserModel:model];
        [self.topView SQ_Data];
        [self SQ_JsonArray];
        self.right.text = [NSString stringWithFormat:@"%d",model.diamond];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_JsonArray{
    [self.jsonArray removeAllObjects];
    NSArray * array = @[@"反馈问题",@"隐私协议",@"关于",@"注销",@"退出登录",@"我的收藏"];
    for (NSString * string in array) {
        [self.jsonArray addObject:string];
    }
   
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeTableViewCell"];
    if (!cell) {
        cell = [[MeTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"MeTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.name =  [self.jsonArray objectAtIndex:indexPath.item];
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  62;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString * name = [self.jsonArray objectAtIndex:indexPath.item];
     if ([name isEqualToString:@"反馈问题"]) {
         FankuiViewController *vc = [[FankuiViewController alloc]init];
         [self SQ_pushViewControllerFull:vc];
    }else  if ([name isEqualToString:@"隐私协议"]) {
        [self SQ_xieYiPopView];
    }else  if ([name isEqualToString:@"关于"]) {
        AboutViewController * vc = [[AboutViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    }else  if ([name isEqualToString:@"注销"]) {
        [self SQ_zhuxiaodenglu];
    }else  if ([name isEqualToString:@"退出登录"]) {
        [self SQ_QuitPopView];
       
    }else  if ([name isEqualToString:@"我的收藏"]) {
        CollectViewController * vc = [[CollectViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
       
    }
}

-(void)SQ_zhuxiaodenglu{
  ZhuxiaoPopView * view = [[ZhuxiaoPopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 300)];
  LSTPopView * pop = [LSTPopView initWithCustomView:view
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleCenter;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [view.rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
         
        [HttpTool post:@"user/logoff" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
            [wk_pop dismiss];
            [UserModel clearLocalUserModel];
            [[JQFMDB shareDatabase] jq_deleteAllDataFromTable:HomeHistoryTable];
            [Manager SQ_removeObjectForKey:@"account"];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[LoginViewController alloc]init]];
             weakSelf.view.window.rootViewController = nav;
        } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
            [YJProgressHUD showMsgWithoutView:httpModel.message];
        }];
    }];
}
-(void)SQ_headerBtoClick{
    HeaderTopView * view = [[HeaderTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 300)];
    LSTPopView * pop = [LSTPopView initWithCustomView:view
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [view setHeaderBlock:^{
        [weakSelf.topView SQ_Data];
        [wk_pop dismiss];
    }];
}
-(void)SQ_QuitPopView{
    QuitView * vc = [[QuitView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleCenter;
    [pop pop];
    LSTPopViewWK(pop)
    [vc.leftBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [vc.rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [UserModel clearLocalUserModel];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[LoginViewController alloc]init]];
         weakSelf.view.window.rootViewController = nav;
    }];
}
-(void)SQ_xieYiPopView{
    PeopleXYView * view = [[PeopleXYView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 464)];
    LSTPopView * pop = [LSTPopView initWithCustomView:view
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleCenter;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
}


- (TopMeView *)topView{
    if (!_topView) {
        _topView = [[TopMeView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_WIDETH*230/375)];
        [_topView.avatarImageView.btn addTarget:self action:@selector(SQ_headerBtoClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _topView;
}

- (SQCustomButton *)right{
    if (!_right) {
        _right = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(20, 22) midmargin:6];
        _right.image = SQGetImage(@"金币");
        _right.text  = @"0";
        _right.font = FONT_MEDIUM(14);
        _right.textColor = SQColor333333;
    }
    return _right;
}
@end
